package chapterSystem.client;

import chapterSystem.client.interfaceDrawing.ControlsDrawer;
import chapterSystem.client.model.entitiesInt.Brother;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyDownEvent;
import com.google.gwt.event.dom.client.KeyDownHandler;
import com.google.gwt.user.client.Cookies;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.*;

/**
 * Entry point classes define <code>onModuleLoad()</code>
 */
public class ChapterSystem implements EntryPoint {
    public static String COOKIE_NAME = "CS_auth";

    private ControlsDrawer controlsDrawer;

    public void onModuleLoad() {
        ChapterSystemService.App.getInstance().checkCookie(Cookies.getCookie(COOKIE_NAME), new CookieCallback());
    }


    private void drawControls(Brother brother) {
        RootPanel.get("auth").clear();
        controlsDrawer = new ControlsDrawer.ControlsDrawerBuilder(RootLayoutPanel.get()).setName(brother.getName()).
                setRankWork(brother.getRankBattle()).setRankBattle(brother.getRankBattle()).
                setPoints(brother.getPoints()).setId(brother.getId()).setUnitId(brother.getUnitId()).build();
    }

    private void drawAuth() {
        final VerticalPanel authPanel = new VerticalPanel();


        final TextBox usernameField = new TextBox();
        final PasswordTextBox passwordField = new PasswordTextBox();
        final Button loginButton = new Button("Login");
//        final HTML loginLabel = new HTML("<h3 style=\"color:#ffffff; backgroung-image:url('wall.jpg')\">Please Login</h3>");

        loginButton.addClickHandler(new ClickHandler() {
            @Override
            public void onClick(ClickEvent event) {
                ChapterSystemService.App.getInstance().login(usernameField.getText(), passwordField.getText(),
                        new AuthCallback());
            }
        });
        passwordField.addKeyDownHandler(new KeyDownHandler() {
            @Override
            public void onKeyDown(KeyDownEvent event) {
                if (event.getNativeKeyCode() == 13) {//Enter
                    loginButton.click();
                }
            }
        });


//        final HorizontalPanel loginLabelPanel = new HorizontalPanel();
        final FlowPanel loginLabelPanel = new FlowPanel();
        final Label loginLabel = new Label("Please Login");
        loginLabel.setStyleName("login_style");
        loginLabel.setWidth("100%");
        loginLabelPanel.setStyleName("login_style");
        loginLabelPanel.add(loginLabel);
//        loginLabel.setStyleName("login_style");
//        loginLabelPanel.setCellWidth(loginLabel, "100%");
//        loginLabelPanel.setCellHeight(loginLabel, "100%");
//        loginLabelPanel.setCellVerticalAlignment(loginLabel, HasVerticalAlignment.ALIGN_MIDDLE);
//        loginLabelPanel.setCellHorizontalAlignment(loginLabel, HasAlignment.ALIGN_CENTER);

        final FlowPanel usernamePanel = new FlowPanel();
        usernamePanel.setStyleName("login_style");
        usernamePanel.add(usernameField);
        usernamePanel.setSize("100%", "100%");

        final FlowPanel passwordPanel = new FlowPanel();
        passwordPanel.setStyleName("login_style");
        passwordPanel.add(passwordField);
        passwordPanel.setSize("100%", "100%");

        final HorizontalPanel buttonPanel = new HorizontalPanel();
        buttonPanel.setStyleName("login_style");
        buttonPanel.add(loginButton);
        buttonPanel.setSize("100%", "100%");
        buttonPanel.setCellVerticalAlignment(loginButton, HasVerticalAlignment.ALIGN_MIDDLE);
        buttonPanel.setCellHorizontalAlignment(loginButton, HasAlignment.ALIGN_CENTER);

        authPanel.add(loginLabelPanel);
        authPanel.add(usernamePanel);
        authPanel.add(passwordPanel);
        authPanel.add(buttonPanel);

        authPanel.setCellHorizontalAlignment(buttonPanel, HasAlignment.ALIGN_CENTER);
        authPanel.setCellHorizontalAlignment(loginLabelPanel, HasAlignment.ALIGN_CENTER);
        authPanel.setVerticalAlignment(HasVerticalAlignment.ALIGN_MIDDLE);
        authPanel.setStyleDependentName("login_style", true);

        RootPanel.get("auth").add(authPanel);
    }

    private class AuthCallback implements AsyncCallback<Brother> {

        @Override
        public void onFailure(Throwable caught) {
            caught.printStackTrace();
            Window.alert(caught.getMessage());
        }

        @Override
        public void onSuccess(Brother brother) {
            if (brother != null) {
                drawControls(brother);
                Cookies.setCookie(COOKIE_NAME, brother.getCookie());
            } else {
                Window.alert("Wrong username or password");
            }
        }
    }

    private class CookieCallback implements AsyncCallback<Brother> {

        @Override
        public void onFailure(Throwable caught) {
            drawAuth();
        }

        @Override
        public void onSuccess(Brother brother) {
            if (brother == null) {
                drawAuth();
            } else {
                drawControls(brother);
            }
        }
    }
}
